// sharing/pages/applet/applet.js
var app = getApp();
var page = 0;
var isload = false;
Page({
	TOAPPID: require('../../resource/components/footer/footer.js'),
  /**
   * 页面的初始数据
   */
  data: {
	  cid: 0,
	  keyword: '',
	  hidePic: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	  var that = this;
	  app.footerInit(that);
	  page = 0;
	  isload = false;
	  app.util.request({
		  url: 'entry/wxapp/applet',
		  data: {op: 'index', m: 'junsion_wifinav'},
		  cachetime: 0,
		  success: function(res){
			  var data = res.data;
			  if(data.slider){
				  that.setData({slider: data.slider});
			  }
			  if(data.recommend){
				  that.setData({recommend: data.recommend});
			  }
			  if(data.category){
				  that.setData({category: data.category});
			  }
        wx.setNavigationBarTitle({
          title: res.data.title ? res.data.title:'小程序大全',
        })
		  }
	  });
	  that.setData({keyword: '', cid: 0});
	  that.loadList();
	  app.getShareData(function(res){that.setData({cfg: res.data})});
  },
  loadList: function(){
	  if(isload) return;
	  isload = true;
	  page++;
	  var that = this;
	  var cid = that.data.cid;
	  var keyword = that.data.keyword;
	  wx.showLoading({title: '读取中...'});
	  app.util.request({
		  url: 'entry/wxapp/applet',
		  data: {page: page, keyword: keyword, cid: cid,op: 'getList', m: 'junsion_wifinav'},
		  cachetime: 0,
		  success: function(res){
			  wx.hideLoading();
			  var data = res.data;
			  if(data.code){
          that.setData({ list: data.list});
				  isload = false;
			  }
		  }
	  });
  },
  selCate: function(e){
	  this.setData({cid: e.currentTarget.dataset.index, list: ''});
	  isload = false;
	  page = 0;
	  this.loadList();
  },
  closeSearch: function(){
	  this.setData({keyword: ''});
  },
  search: function(e){
	  this.setData({cid: 0, keyword: e.detail.value.keyword, list: ''});
	  isload = false;
	  page = 0;
	  this.loadList();
  },
  onRand: function(){
    page++;
    this.loadList();
  },
  clickItem: function(e){
	  var mtype = e.currentTarget.dataset.mtype;
	  var t = e.currentTarget.dataset.type;
	  var idx = e.currentTarget.dataset.idx;
	  if(mtype == 'slider'){
		  var list = this.data.slider;
	  }else if(mtype == 'recommend'){
		  var list = this.data.recommend;
	  }else if(mtype == 'list'){
		  var list = this.data.list;
	  }
	  
	  if(t==1){
		  wx.navigateToMiniProgram({
			  appId: list[idx].appid,
			  path: list[idx].path,
			  fail: function(r){
				  console.log(r);
			  }
		  });
	  }else if (t == 2){
		  this.setData({qrlist: list[idx], hidePic: false});
	  }else if(t == 3){
      wx.setStorageSync('advurl', list[idx].path);
		  wx.navigateTo({
			  url:'../webview/webview',
		  });
	  }
  },
  hidePic: function(){
	  this.setData({hidePic: true});
  },
  previewPic: function(){
	  var that = this;
	  wx.previewImage({  
      current: that.data.qrlist.qrcode, // 当前显示图片的http链接   
      urls: [that.data.qrlist.qrcode]
	  }) 
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },
  onShareAppMessage: function () {
	  return {
	      title: this.data.cfg.share.appletShare,
	      path: '/sharing/pages/applet/applet',
	      imageUrl: this.data.cfg.share.appletPic,
	      success: function(res) {
	        // 转发成功
	      },
	      fail: function(res) {
	        // 转发失败
	      }
	    }
  },
  onPullDownRefresh: function(){
	  this.onLoad();
	  wx.stopPullDownRefresh();
  }
})